/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.backend;

import org.json.JSONObject;

import org.cholm.media.castdr.LogUtils; import static org.cholm.media.castdr.LogUtils.LOGd;

/**
 * @author cholm
 */
public class LiveChannel extends SubCategory {
    /**
     * Debug tag
     */
    private static final String TAG = LogUtils.makeLogTag(LiveChannel.class);
    /**
     * Current show
     */
    public Broadcast mNow = new Broadcast();
    /**
     * Next show
     */
    public Broadcast mNext = new Broadcast();
    /**
     * Our stream
     */
    public String mStream = null;
    /**
     * Image url
     */
    public String mImage = "";
    /**
     * Time (in seconds) to next
     */
    public int mToNext = 0;

    /**
     * @param c      Parent category
     * @param name   Name of this live channel
     * @param url    Url to query for information
     * @param img    Image url
     * @param stream The actual live stream
     */
    public LiveChannel(Category c, String name, String url, String img, String stream) {
        super(c, name, url);
        mStream = stream;
        mImage = img;
    }

    /**
     * Load this
     */
    public boolean load() {
        LOGd(TAG, "Loading ...");
        Extractor e = Extractor.instance();
        JSONObject json = e.getJSONObject(mUrl);
        if (json == null) {
            // Got nothing, perhaps we need a reload here?
            return false;
        }

        mNow.update("", "Ingen program informationer", "");
        mNext.update("", "", "");

        JSONObject now = json.optJSONObject("Now");
        JSONObject next = json.optJSONObject("Next");
        if (now != null && next != null) {
            mNow.update(now, true);
            mNext.update(next, false);

            int cur = json.optInt("CurrentServerTimestamp", 0);
            int end = now.optInt("EndTimestamp", -1);
            // Get the duration until the next in seconds
            mToNext = (end == -1 ? -1 : (end - cur) / 1000);
        }
        return true;
    }

    public Broadcast getNow() {
        return mNow;
    }

    public Broadcast getNext() {
        return mNext;
    }

    public String getStream() {
        return mStream;
    }

    public String getImage() {
        return mImage;
    }

    /**
     * Broadcast information
     */
    public class Broadcast {
        /**
         * Title
         */
        public String mTitle = "";
        /**
         * Start time
         */
        public String mStart = "";
        /**
         * Description
         */
        public String mDesc = "";

        /**
         * Update the information
         *
         * @param t Title
         * @param s Start time
         * @param d Description
         */
        public void update(String t, String s, String d) {
            mTitle = t;
            mStart = s;
            mDesc = d;
        }

        /**
         * Update from JSON
         *
         * @param json JSON data
         * @param desc Optional fill in description too
         */
        public void update(JSONObject json, boolean desc) {
            if (json == null) update("", "-", "");
            mTitle = json.optString("Title");
            mStart = json.optString("FormattedBroadcastTime");
            mDesc = (desc ? json.optString("Description") : "");
        }
    }
}
